<?php
header("content-type:text/html;charset=utf-8");
error_reporting(E_ALL);
ini_set('display_errors',1);
set_time_limit(0);
date_default_timezone_set('Asia/Shanghai');

define('IS_AJAX', isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtolower($_SERVER["HTTP_X_REQUESTED_WITH"]) == 'xmlhttprequest');

if(IS_AJAX){
	isset($_GET['action']) or $_GET['action']='';
	switch($_GET['action']){
		case 'run':
			ob_start();
			eval($_POST['code']);
			$____result=ob_get_contents();
			ob_end_clean();
			echo json_encode(array('code'=>1,'result'=>$____result));
			exit;
		break;
		case 'phpinfo':
			ob_start();
			phpinfo();
			$result=ob_get_contents();
			ob_end_clean();
			echo json_encode(array('code'=>1,'result'=>$result));
			exit;
		break;
		case 'source':
			echo json_encode(array('code'=>1,'result'=>htmlspecialchars($_POST['result'],ENT_QUOTES,'UTF-8')));
			exit;
		break;
		case 'fragment':
			$result=file_get_contents('./fragment/'.$_POST['file']);
			$result=str_replace('<?php','',$result);
			$result=str_replace('?>','',$result);
			$result=trim($result);
			echo json_encode(array('code'=>1,'result'=>$result));
			exit;
		break;
		default:
			echo json_encode(array('code'=>0,'result'=>''));
			exit;
		break;
	}
}
$code=<<<'st'
echo "hello world!";
st;


$fragments=array();
$dir='./fragment/';
if ($dh=opendir($dir)){
	while(($file=readdir($dh))!==false) {
		if($file=='.' || $file=='..') continue;
		$fragments[]=$file;
	}
	closedir($dh);
}

?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>php在线运行工具</title>
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
</head>

<body>
<h2 style="text-align:center">php在线运行工具</h2>
<table width="100%" border="1">
  <tbody>
    <tr>
      <td align="center"><textarea name="code" id="code" cols="30" rows="10" style="width:100%;height:400px;"><?php echo $code;?></textarea></td>
    </tr>
    <tr>
      <td align="center">
      <select name="fragment" id="fragment">
      	<option value="">代码片断</option>
        <?php
        foreach($fragments as $k=>$v){
		?>
        <option value="<?php echo $v;?>"><?php echo $k;?> : <?php echo $v;?></option>
        <?php
		}
		?>
      </select>
      </td>
    </tr>
    <tr>
      <td align="center"><input type="button" value="运行" id="run" style="width:19%;height:100px;border:3px solid #E81D20;background:#12E76C;">&nbsp;<input type="button" value="清空" id="clear" style="width:19%;height:100px;border:3px solid #E90BE8;background:#E1C820;">&nbsp;<input type="button" value="phpinfo" id="phpinfo" style="width:19%;height:100px;border:3px solid #E90BE8;background:#074BD5;">&nbsp;<input type="button" value="格式化" id="format" style="width:19%;height:100px;border:3px solid #231A1A;background:#C429CD;">&nbsp;<input type="button" value="源代码" id="source" style="width:19%;height:100px;border:3px solid #0D66D0;background:green;"></td>
    </tr>
    <tr>
      <td align="center"><div id="result" style="width:100%;height:400px;overflow:scroll;"></div></td>
    </tr>
  </tbody>
</table>
<script src="js/jquery.js"></script>
<script>
$(document).ready(function(e) {
	$('#fragment').change(function(){
		var v=$(this).val();
		if(!v){ 
			$('#code').val('');
			return;
		}
		$.post('?action=fragment',{file:v},function(data){
			$('#code').val(data.result);
		},'json');
			
	});
	
    $('#run').click(function(){
		var code=$('#code').val();
		if(!code) return;
		$.post('?action=run',{code:code},function(data){
			$('#result').html(data.result);
		},'json');
	});
	$('#clear').click(function(){
		if(!confirm('确定要清空么？')) return;
		$('#code').val('');	
		$('#result').html('');
	});
	$('#phpinfo').click(function(){
		$.post('?action=phpinfo',{code:''},function(data){
			$('#result').html(data.result);
		},'json');
	});
	$('#format').click(function(){
		$('#result').html('<pre style="text-align:left;">'+$('#result').html()+'</pre>');
	});
	$('#source').click(function(){
		var result=$('#result').html();
		if(!result) return;
		$.post('?action=source',{result:result},function(data){
			$('#result').html(data.result);
		},'json');	
	});
});
</script>

</body>
</html>